package com.happycat.service.impl;
 
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happycat.Pagination;
import com.happycat.ResponseHeaderModel;
import com.happycat.ResponseModel;
import com.happycat.SysContants;
import com.happycat.mapper.UserVisitMapper;
import com.happycat.mobile.model.UserVisit;
import com.happycat.mobile.service.GenerateIdService;
import com.happycat.mobile.service.UserVisitService;
import com.happycat.utils.ResponseModelUtils;
/**
 * Module: UserVisit.java
 * 
 * @author liaoyong
 * @since JDK 1.8
 * @version 1.0
 * @date 2020-08-02
 * @Descriptions:
 */
@Service
public class UserVisitServiceImpl extends ServiceImpl<UserVisitMapper, UserVisit> implements UserVisitService {
 	
	private GenerateIdService generateIdService;
	
 	public ResponseModel<UserVisit> selectOne(UserVisit userVisit){
 		Wrapper<UserVisit> queryWrapper = new QueryWrapper<>(userVisit);
		ResponseModel<UserVisit> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(getOne(queryWrapper));
		return responseModel;
 	
 	}
	
	public ResponseHeaderModel	add(UserVisit userVisit){
		ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();		
		userVisit.setId(generateIdService.nextDefaultSerialNumber());
		userVisit.setCreateTime(new Date());
		boolean result = save(userVisit);
		if(result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("新增成功");
		}else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("新增失败");
		}
		return responseHeaderModel;
	}
	
	public ResponseHeaderModel	modify(UserVisit userVisit){
			ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();
		
		boolean result = updateById(userVisit);
		if(result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("修改成功");
		}else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("修改失败");
		}
		return responseHeaderModel;
	}
	
	public ResponseModel<Page<UserVisit>> pageList(Pagination<UserVisit>page){
		ResponseModel<Page<UserVisit>> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");		
		responseModel.setData(this.baseMapper.pageList(page));
		return responseModel;
	}

	@Override
	public ResponseModel<UserVisit> getCount(UserVisit userVisit) {				
		if(StringUtils.isNotBlank(userVisit.getAccountId()+"")) {
			
			QueryWrapper<UserVisit> queryWrapper = new QueryWrapper<UserVisit>();
			queryWrapper.eq("visit_id", userVisit.getAccountId());
			int count= count(queryWrapper);
			userVisit.setFootmarkNumber(count);
			QueryWrapper<UserVisit> queryWrapperV = new QueryWrapper<UserVisit>();
			queryWrapperV.eq("account_id", userVisit.getAccountId());
			int total= count(queryWrapperV);
			userVisit.setVisitedNumber(total);
			return ResponseModelUtils.success("查询成功", userVisit);
		}
		return ResponseModelUtils.fail("参数格式不正确", null);
	}
}
